﻿var macro;
var jsLF = "\n";
var i, retcode, errtext;
var MyDelay;
var MyPosts;

var Aldeias;

var espioes;
var cavalariaLeve;
var command;

i = 0;

espioes = 1;
cavalariaLeve = 4;
//command = commandPega();



Aldeias = new Array();

Aldeias.push("32667");
Aldeias.push("31384");
Aldeias.push("32212");
Aldeias.push("34672");
Aldeias.push("31124");
Aldeias.push("31055");
Aldeias.push("32265");
Aldeias.push("35798");
Aldeias.push("32127");
Aldeias.push("33375");
Aldeias.push("32237");
Aldeias.push("32216");
Aldeias.push("31963");
Aldeias.push("35868");
Aldeias.push("33794");
Aldeias.push("30989");
Aldeias.push("34853");
Aldeias.push("30747");
Aldeias.push("34008");
Aldeias.push("34714");
Aldeias.push("30044");
Aldeias.push("30966");
Aldeias.push("34412");
Aldeias.push("35098");
Aldeias.push("30909");
Aldeias.push("31920");
Aldeias.push("30068");
Aldeias.push("30634");
Aldeias.push("32269");
Aldeias.push("34498");
Aldeias.push("34728");
Aldeias.push("32763");
Aldeias.push("34363");
Aldeias.push("35847");
Aldeias.push("34502");
Aldeias.push("32933");
Aldeias.push("33382");
Aldeias.push("34343");
//Aldeias.push("30746");
Aldeias.push("35623");
Aldeias.push("35708");
Aldeias.push("36313");
Aldeias.push("34638");
Aldeias.push("31702");
Aldeias.push("34858");
Aldeias.push("30137");
Aldeias.push("30931");
Aldeias.push("36886");
Aldeias.push("31383");
Aldeias.push("34537");
Aldeias.push("34879");
Aldeias.push("35597");
Aldeias.push("32219");
Aldeias.push("33925");
Aldeias.push("34655");
Aldeias.push("29929");
Aldeias.push("34405");
Aldeias.push("30169");
Aldeias.push("35277");
Aldeias.push("35899");
Aldeias.push("30268");
Aldeias.push("36226");
Aldeias.push("28316");
Aldeias.push("30880");
Aldeias.push("35269");








//Q0
//0.1

a1 = "32667";
if(!conteins(a1)){
   Aldeias.push(a1);
}
//0.2
a1 = "31384";
if(!conteins(a1)){
   Aldeias.push(a1);
}
//0.3
a1 = "34672";
if(!conteins(a1)){
   Aldeias.push(a1);
}
//0.4
a1 = "32212";
if(!conteins(a1)){
   Aldeias.push(a1);
}
//0.5
a1 = "31055";
if(!conteins(a1)){
   Aldeias.push(a1);
}
//0.6
a1 = "31124";
if(!conteins(a1)){
   Aldeias.push(a1);
}
//0.7
a1 = "32265";
if(!conteins(a1)){
   Aldeias.push(a1);
}
//0.8
a1 = "35798";
if(!conteins(a1)){
   Aldeias.push(a1);
}
//Q1
//1.1
a1 = "31963";
if(!conteins(a1)){
   Aldeias.push(a1);
}
//1.2
a1 = "32237";
if(!conteins(a1)){
   Aldeias.push(a1);
}
//1.3
a1 = "32216";
if(!conteins(a1)){
   Aldeias.push(a1);
}
//1.4
a1 = "30989";
if(!conteins(a1)){
   Aldeias.push(a1);
}
//1.5
a1 = "30747";
if(!conteins(a1)){
   Aldeias.push(a1);
}
//1.6
a1 = "30966";
if(!conteins(a1)){
   Aldeias.push(a1);
}
//1.7
a1 = "30044";
if(!conteins(a1)){
   Aldeias.push(a1);
}
//1.8
a1 = "30068";
if(!conteins(a1)){
   Aldeias.push(a1);
}
//1.9
a1 = "31383";
if(!conteins(a1)){
   Aldeias.push(a1);
}
//Q2//2.1
a1 = "30746";
if(!conteins(a1)){
   Aldeias.push(a1);
}
//2.2
a1 = "31702";
if(!conteins(a1)){
   Aldeias.push(a1);
}
//2.3
a1 = "30634";
if(!conteins(a1)){
   Aldeias.push(a1);
}


//2.6
a1 = "31174";
if(!conteins(a1)){
   Aldeias.push(a1);
}
//2.8
a1 = "30169";
if(!conteins(a1)){
   Aldeias.push(a1);
}
//2.9
a1 = "31506";
if(!conteins(a1)){
   Aldeias.push(a1);
}
//2.10
a1 = "28316";
if(!conteins(a1)){
   Aldeias.push(a1);
}
//2.11
a1 = "30594";
if(!conteins(a1)){
   Aldeias.push(a1);
}
//2.12
a1 = "29929";
if(!conteins(a1)){
   Aldeias.push(a1);
}
//2.13
a1 = "30268";
if(!conteins(a1)){
   Aldeias.push(a1);
}
//2.14
//a1 = "29281";
//Q3//3.1
a1 = "32127";
if(!conteins(a1)){
   Aldeias.push(a1);
}
//3.2
a1 = "34412";
if(!conteins(a1)){
   Aldeias.push(a1);
}
//3.3
a1 = "32269";
if(!conteins(a1)){
   Aldeias.push(a1);
}
//3.4
a1 = "33382";
if(!conteins(a1)){
   Aldeias.push(a1);
}
//3.5
a1 = "32219";
if(!conteins(a1)){
   Aldeias.push(a1);
}
//3.6
a1 = "30931";
if(!conteins(a1)){
   Aldeias.push(a1);
}
//3.7
a1 = "33925";
if(!conteins(a1)){
   Aldeias.push(a1);
}
//3.8
a1 = "31615";
if(!conteins(a1)){
   Aldeias.push(a1);
}
//Q4//4.1
a1 = "34502";
if(!conteins(a1)){
   Aldeias.push(a1);
}
//4.2
a1 = "34638";
if(!conteins(a1)){
   Aldeias.push(a1);
}
//4.3
//a1 = "36709";
//4.4
a1 = "34537";
if(!conteins(a1)){
   Aldeias.push(a1);
}
//4.5
a1 = "35277";
if(!conteins(a1)){
   Aldeias.push(a1);
}

//4.7
a1 = "35269";
if(!conteins(a1)){
   Aldeias.push(a1);
}

//Q5
//5.1
a1 = "33794";
if(!conteins(a1)){
   Aldeias.push(a1);
}
//5.2
a1 = "34853";
if(!conteins(a1)){
   Aldeias.push(a1);
}
//5.3
a1 = "34714";
if(!conteins(a1)){
   Aldeias.push(a1);
}
//5.4
a1 = "35847";
if(!conteins(a1)){
   Aldeias.push(a1);
}
//5.5
a1 = "36313";
if(!conteins(a1)){
   Aldeias.push(a1);
}
//5.6
a1 = "35708";
if(!conteins(a1)){
   Aldeias.push(a1);
}
//5.7
a1 = "34343";
if(!conteins(a1)){
   Aldeias.push(a1);
}
//5.8
a1 = "36886";
if(!conteins(a1)){
   Aldeias.push(a1);
}
//5.9
a1 = "34655";
if(!conteins(a1)){
   Aldeias.push(a1);
}
//5.10
a1 = "37651";
if(!conteins(a1)){
   Aldeias.push(a1);
}
//5.11
a1 = "38779";
if(!conteins(a1)){
   Aldeias.push(a1);
}
//Q6
//6.1
a1 = "36496";
if(!conteins(a1)){
   Aldeias.push(a1);
}
//6.2
a1 = "34728";
if(!conteins(a1)){
   Aldeias.push(a1);
}
//6.3
a1 = "35597";
if(!conteins(a1)){
   Aldeias.push(a1);
}
//6.4
a1 = "35253";
if(!conteins(a1)){
   Aldeias.push(a1);
}
//6.5
a1 = "35899";
if(!conteins(a1)){
   Aldeias.push(a1);
}
//6.6
a1 = "34858";
if(!conteins(a1)){
   Aldeias.push(a1);
}
//6.7
a1 = "39146";
if(!conteins(a1)){
   Aldeias.push(a1);
}
//6.8
a1 = "36226";
if(!conteins(a1)){
   Aldeias.push(a1);
}
//Q7
//7.1
a1 = "33375";
if(!conteins(a1)){
   Aldeias.push(a1);
}
//7.2
a1 = "35868";
if(!conteins(a1)){
   Aldeias.push(a1);
}
//7.3
a1 = "31920";
if(!conteins(a1)){
   Aldeias.push(a1);
}
//7.4
a1 = "34008";
if(!conteins(a1)){
   Aldeias.push(a1);
}
//7.5
a1 = "35098";
if(!conteins(a1)){
   Aldeias.push(a1);
}
//7.6
a1 = "34498";
if(!conteins(a1)){
   Aldeias.push(a1);
}
//7.7
a1 = "34363";
if(!conteins(a1)){
   Aldeias.push(a1);
}
//7.8
a1 = "32933";
if(!conteins(a1)){
   Aldeias.push(a1);
}
//7.9
a1 = "34879";
if(!conteins(a1)){
   Aldeias.push(a1);
}
//7.10
a1 = "35623";
if(!conteins(a1)){
   Aldeias.push(a1);
}
//7.11
a1 = "33389";
if(!conteins(a1)){
   Aldeias.push(a1);
}
//7.12
a1 = "34395";
if(!conteins(a1)){
   Aldeias.push(a1);
}
//Q8
//8.1
a1 = "30909";
if(!conteins(a1)){
   Aldeias.push(a1);
}
//8.2
a1 = "32763";
if(!conteins(a1)){
   Aldeias.push(a1);
}
//8.3
a1 = "29980";
if(!conteins(a1)){
   Aldeias.push(a1);
}
//8.4
a1 = "30880";
if(!conteins(a1)){
   Aldeias.push(a1);
}
//8.5
a1 = "29734";
if(!conteins(a1)){
   Aldeias.push(a1);
}





var log =  "";

MyDelay = 0;

for (i ; i < Aldeias.length; i++) {
    
    log = "Executando "+i+ " de " + (Aldeias.length -1) +jsLF + log;
    
    iimDisplay(log);
    
    iimSet("POSTID", Aldeias[i]);
    
    macro = "CODE:";
    macro += "URL GOTO=http://br37.tribalwars.com.br/game.php?village=31611&screen=place&target={{postid}}" + jsLF;
    macro += "TAG POS=1 TYPE=TD ATTR=ID:inner-border EXTRACT=HTM";
    iimPlay(macro);

    var text = iimGetLastExtract();
     
    var numCavalariaLeve = text.substr(text.indexOf("('#unit_input_light')")+23,10);
    numCavalariaLeve = numCavalariaLeve.substr(0,numCavalariaLeve.indexOf(")"));
    numCavalariaLeve = parseInt(numCavalariaLeve);
    //  alert(numCavalariaLeve);

    if(numCavalariaLeve<cavalariaLeve){
        //alert("não ha cavalos suficientes. Necessário "+cavalariaLeve);
        macro = "CODE:";
        macro += "WAIT SECONDS=60" + jsLF;
        iimPlay(macro);
        i--
        continue;
    }

    var numSpy = text.substr(text.indexOf("('#unit_input_spy')")+20,10);
    numSpy = numSpy.substr(0,numSpy.indexOf(")"));
    numSpy = parseInt(numSpy);
    //    alert(numSpy);

    if(numSpy<espioes){
        //alert("não ha espioes. Necessário "+espioes);
        macro = "CODE:";
        macro += "WAIT SECONDS=60" + jsLF;
        iimPlay(macro);
        i--
        continue;

    }


    macro = "CODE:";
    macro += "TAG POS=1 TYPE=INPUT:TEXT FORM=NAME:units ATTR=ID:unit_input_spy CONTENT="+ espioes + jsLF;
    macro += "TAG POS=1 TYPE=INPUT:TEXT FORM=NAME:units ATTR=ID:unit_input_light CONTENT=" + cavalariaLeve + jsLF;
    macro += "TAG POS=1 TYPE=INPUT:SUBMIT FORM=ID:units_form ATTR=ID:target_attack" + jsLF;
    macro += "TAG POS=1 TYPE=TD ATTR=ID:inner-border EXTRACT=HTM";
    
    iimPlay(macro);

    text = iimGetLastExtract();
    command = text.substr(text.indexOf("action=command&amp;h=")+21,4);
    
    macro = "CODE:";
    macro += "TAG POS=1 TYPE=INPUT:SUBMIT FORM=ACTION:http://br37.tribalwars.com.br/game.php?village=31611&screen=place&action=command&h="+ command +" ATTR=ID:troop_confirm_go" + jsLF;
    macro += "WAIT SECONDS=" + MyDelay + jsLF;
    

    retcode = iimPlay(macro);
    if (retcode < 0) {              // an error has occured
        errtext = iimGetLastError();
        alert(errtext);
        i--;

    //commandPega();
        
    // break;
    }
}







function commandPega(){
    var macro = "CODE:";
    macro += "URL GOTO=http://br37.tribalwars.com.br/game.php?village=31611&screen=place&target=32667" + jsLF;
    macro += "TAG POS=1 TYPE=INPUT:TEXT FORM=NAME:units ATTR=ID:unit_input_spy CONTENT="+ espioes + jsLF;
    macro += "TAG POS=1 TYPE=INPUT:TEXT FORM=NAME:units ATTR=ID:unit_input_light CONTENT=" + cavalariaLeve + jsLF;
    macro += "TAG POS=1 TYPE=INPUT:SUBMIT FORM=ID:units_form ATTR=ID:target_attack" + jsLF;
    //macro += "TAG POS=1 TYPE=BODY ATTR=CLASS:* EXTRACT=HTM";
    macro += "TAG POS=1 TYPE=TD ATTR=ID:inner-border EXTRACT=HTM";
	
    var retcode = iimPlay(macro);

    //retcode = iimPlay(macro);
    	
    var text = iimGetLastExtract();
    text = text.substr(text.indexOf("action=command&amp;h=")+21,4);
    alert(text);
    
    return text;
}

function conteins(aldeia){
    var i;
    for(i = 0; i<Aldeias.length;i++){
        if(Aldeias[i]== aldeia)
            return true;
    }
    return false;
}
